/*->c.vtdef */

#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <signal.h>
#include <ctype.h>
#include <time.h>

#include "h.os"
#include "h.wimp"



#include "h.wos"
#include "h.main"
#include "h.ram"
#include "h.mym"
#include "h.main"
#include "h.trans"

#include "h.term"


#include "h.vtdef"


#include "h.vtact"
#include "h.vtkey"
#include "h.vtscr"
#include "h.vtwimp"
#include "h.vtcon"
#include "h.vtlo"
#include "h.vtcol"
#include "h.vtchar"
#include "h.vtselect"

/***************************************************************************/
/*
 Code to handle VT terminals global variables
*/
/***************************************************************************/


#define MINVTWHEIGHT  80
#define MINVTWWIDTH   128
  


int ttyx;                                /* cursor x posn */
int ttyy;                                /* cursor y posn */


int  ttymode;                            /* emulating teletype */
int  ansisys;                            /* ansi.sys mode ?    */
int  vt220;                              /* vt220 mode ?       */

int  scltop;                             /* coord 0-23 of top of scroll zone */
int  sclbot;                             /* coord 0-23 of bot of scroll zone */
int  tabs[TTYCOLS];                      /* array of tabs */
int  width;                              /* dynamic width 80/132 */

int  altkey;                             /* alternate keypad state */
int  ansimode;                           /* VT52 emulating VT100 */

int  newlinemode;
int  wrapmode;
int  keypadmode;                         /*   set = application mode */
int  keylockmode;                        /*   set = keyboard locked */
int  cursorkeymode;                      /* 1 determines cursor key codes */
int  screenmode;                         /* 5 set=reverse video */
int  originmode;                         /* 6 set=origin top of scroll zone - 
                                            can't leave scroll zone  */
int  automode;                           /* 8 set=autorepeat */
int  printer;                            /* does the printer exist */
int  printff;                            /* print form feed ? */
int  printarea;                          /* 1=full screen 0 =scroll zone*/
int  autoprint;                          /* 1=send stuff to printer */
int  conoprint;                          /* 1=only send stuff to printer */
int  echomode;                           /* echo mode */
int  insertmode;                         /* insert mode */
int  txcrlnf;                            /* 1 = map 13's to 13,10 seq's */
int  rxcrlnf;                            /* 1 = map 13's to 13,10 seq's */
int  destbkspc;                          /* 1 = destructive backspace   */
int  ignorerb;                           /* 1 = dont rub  out    */
int  vtswopbs;                           /* 1 = swop bs with del */
int  localmode;                          /* local editing mode */

char vtleds[4];                          /* array for led states */

char vtanswerback[VTABACKLEN];


int  vttekmode;                          /* 1==VT is tek plane */


int  vtncs;                              /* national character set    */
int  vtncsinuse;                         /* in use?                   */

int  vtupss=VTLATIN1;                    /* user preferred char set   */

int  vtssg2;                             /* single shift for gl to g2 */
int  vtssg3;                             /* single shift for gr to g3 */

int  vtgl;                               /* has value 0-3        */
int  vtgr;                               /* has value 0-3        */

int  vtgn[4];                            /* has value 0-VTMAXSET */




int  smooth;

int  selerase;
int  rendition;                          /* text rendition    */
int  foreground;                         /* foreground colour */
int  background;                         /* background colour */


int  bit8c;

int  flock;
int  c1codes;
int  dcs;
int  pend;




vtsaveblock vtsavecursor;
int         vtsaved;        /* saved cursor values */

vtsaveblock vtstatusblock;




int  escflag;
int  bra_flag;         /* [ sequence commenced */
int  inner_char;       /* intermediate char received */
int  inner_par;        /* number of params */
int  inner_stack[32];  /* stack of params */
char inner_prefix[32];
int  inner_tot;        /* next byte to add to string */
int  inner_query;      /* received a ? after ESC [   */
int  curly;            /* ESC ( ) sequence           */
int  curlypc;          /* ESC ( % sequence           */
int  hash;             /* ESC # sequence */
int  percent;          /* ESC % sequence */


int  vtcurs;                             /* is there a vt cursor */
int  vtcursphase;                        /* vt cursor phase 0/1  */
int  vtcurstime;                         /* time to change phase */
int  vtblock;                            /* block cursor         */
int  vtbeep;                             /* beep beep */
int  vtdefbuffsize=256;                  /* size of scroll back  */


int  vtflashphase;
int  vtflashtime;

int  vtopen;                             /* vt windows are open        */
int  vtexists=0;                         /* does it exist              */
int  vtvhi;                              /* is vertical bar hi-lighted */

int  vtpendredraw=0;
int  vtmovecurs=0;

int  vtpendshift=0;
int  vtpendscrolly=0;

int  vtpendsubs=0;
int  vtsuby;
int  vtsubn;
int  vtsubdelta;

int  vtselect;                          /* flag is there a zone ? */

int  vtinvert;                          /* B<->W */


char * vtname;                          /* real name of VT */


int tscrolly=0;          /* equal to scy for vertical scroll bar */

int buffsize=0;          /* number of lines in capture buffer             */
int tops=0;              /* offset of line 0 of screen in capture buffer  */

int bscroll=0;           /* offset of line 0 of capture buffer in indexs  */

char * linedata=NULL;    /* buffer holding data for each line on screen   */
char * lineindex=NULL;   /* buffer holding info about each line and index */
                         /* into linedata buffer                          */


int front;
int back;
int style;

int sfront;
int sback;
int sstyle;

zoomer vtzoom={1,1,1};

int    vtoscharx;
int    vtoschary;

int    vtheight;      /* 24/25 */
int    vtstatus;      /* status line mode */

int    vtx0;
int    vtx1;
int    vty0;
int    vty1;
int    vtscx;
int    vtscy;
int    vtby;
int    vtbx;


char * vtpastebuff;       /* VT clip board */
int    vtpastebufflen;    /* length of it  */

int    vtlock;            /* features lock */

int    vtvert=1;          /* using vertical bar ? */


/*****************************************************************************/

/* called once when program starts */

void vtinitboot(void)
{
 keypadmode=0;
 cursorkeymode=1;
 keylockmode=0;
 automode=1;
 altkey=0;
 smooth=0;
 printff=0;
 printarea=0;
 echomode=0;
 insertmode=0;
 newlinemode=0;
 wrapmode=1;
 txcrlnf=0;
 rxcrlnf=0;
 destbkspc=0;
 localmode=0;
 screenmode=0;
 autoprint=0;
 width=80;
 vtheight=24;
 vtstatus=0;
 ignorerb=1;

 vtgn[0]=VTASCII;
 vtgn[1]=VTASCII;
 vtgn[2]=VTLATIN1;
 vtgn[3]=VTLATIN1;

 settabs();
}




/* soft boot */

void vtsoftboot(void)
{
 vtcurs=1;

 vtgl=0;
 vtgr=2;
 vtssg2=0;
 vtssg3=0;
 c1codes=bit8c;
 dcs=0;
}


/* hard boot */

void vthardboot(void)
{ 
 flagset();
 scltop=0;
 sclbot=23;

 originmode=0;
 conoprint=0;
 foreground=WHITE;
 background=BLACK;
 rendition=0;
 setsys();

 ansimode=1;

 ledsoff();
 vtsaved=0;
 vttekmode=0;
 setvt220k();
 vtsoftboot();
}



void vtreset(void)
{
 vthardboot();
 clearscrhome(VTFXNONE);
}



void vtclearpend(void)
{
 vtpendredraw=0;
 vtmovecurs=0;
 vtpendshift=0;
 vtpendscrolly=0;
 vtpendsubs=0;
}



/* this creates a new VT from nothing */
/* return 1 on success                */

int vtcreate(int terminal)
{
 int handle;
 int vertical;
 wimp_wind  * wp;

 if(vtexists) return(1);

/* dprintf(0,"vt create 1"); */

 newcolourmap();

/* dprintf(0,"vt create 2"); */

 vthardboot();    /* sets ansimode to 1 */

/* dprintf(0,"vt create 3"); */


 switch(terminal)
 {
    case TERMTTY:                        /* 80 column TTY defunct */
                 vtname="VT00"; /* Teletype */
                 ttymode=1;
                 vt220=ansisys=ansimode=0;
                 break;

   case TERMVT52:
                 vtname="VT01"; /* VT52 */
                 vt220=ansisys=ttymode=ansimode=0;    
                 break;

  case TERMVT100:
                 vtname="VT02"; /* "VT102"; */
                 vt220=ansisys=ttymode=0;
                 break;

   case TERMANSI:
                 vtname="VT03"; /* "ANSI" */
                 ansisys=1;
                 ttymode=0;
                 vt220=0;      
                 break;

  case TERMVT220:
                 vtname="VT04"; /* "VT320"; */
                 ansisys=ttymode=0;
                 vt220=1;
                 break;
 }


 vtname=transtoken(vtname);

/* dprintf(0,"vt create 5"); */

 if(!bootvtbuffer()) return(0);

 /* vtzoom.mul=vtzoom.div=1; */
 vtsetcharsize();


/* dprintf(0,"vt create 6"); */

 wp=windpoi[VTERM];

 wp->box.x0=0;
 wp->box.x1=screenx-vscrlbar;

/*  wp->box.y0=116;
 wp->box.y1=980;  */

 wp->box.y1=screeny-hscrlbar;
 wp->box.y0=wp->box.y1-vtheight*TTEDY;

 wp->minsize=(MINVTWHEIGHT<<16)+MINVTWWIDTH;

 if(vtvert)
 {
  wp->flags&=~wimp_WVSCR;
  wp->flags|=wimp_WTRESPASS;
 }
 else
 {
  wp->flags|=wimp_WVSCR;
  wp->flags&=~wimp_WTRESPASS;
 }

/* dprintf(0,"vt create 7"); */

 handle=createwindowsub(VTERM,terminalname(vtname));  /* uses zoom */

 vtvhi=1;

 if(vtvert)
 {
  vertical=createwindow(VTVERT);
  extent(handle,0,-vtheight*TTEDY,width*TTEDX,0);
  extent(vertical,0,-buffsize*TTEDY-80,0,0);
 }
 else
 {
  extent(handle,0,-buffsize*TTEDY,width*TTEDX,0);
 }

/* dprintf(0,"vt create 8"); */

 tops=buffsize-vtheight;
 tscrolly=-TTDY*tops;
 ttyx=ttyy=0;
 vtselect=0;
 vtexists=1;

/* dprintf(0,"vt create 9"); */

 vtrefreshstatus();


/* dprintf(0,"vt create 10"); */

 return(1);
}



void vtdestroy(void)
{
 if(vtexists)
 {
  trashvtbuffer();
  closedownt(VTERM);
  if(vtvert) closedownt(VTVERT);
  vtexists=0;
  vttrashpastebuff();
  if(terminalmode==TMODEVT) terminalmode=TMODENUL;
 }
}




/* opens vt window on screen */

void vtopenwindows(int new)
{
 int handle=whandle[VTERM];
 wimp_wstate winds;

 vtopen=1;

/* dprintf(0,"vt open 0"); */

 createtermsprite();

/* dprintf(0,"vt open 1"); */

 newcolourmap();

/* dprintf(0,"vt open 2"); */

 setstylec();

/* dprintf(0,"vt open 3"); */

 setsys();


/* dprintf(0,"vt open 4"); */

 wimp_get_wind_state(handle,&winds);
 
 if(new)
 {
  winds.o.box.x0=0;
  winds.o.box.x1=screenx;

  winds.o.box.y1=screeny-hscrlbar;
  winds.o.box.y0=winds.o.box.y1-vtheight*TTEDY;
  winds.o.behind=-1;

  tscrolly=-TTDY*tops;

  if(!vtvert) winds.o.y=tscrolly;
 }

/* dprintf(0,"vt open 5"); */

 vtrefreshstatus();

/* dprintf(0,"vt open 6"); */

 vtclearpend();

/* dprintf(0,"vt open 7"); */

 vtwimpopenwindows(&winds.o);

/* dprintf(0,"vt open 8"); */

 addzeroevent(VTZERO);
 vtsetfocus();
}




/* closes VT window */

void vtclosewindows(void)
{
 if(vtopen)
 {
  remzeroevent(VTZERO);
  wimp_close_wind(whandle[VTERM]);
  if(vtvert) wimp_close_wind(whandle[VTVERT]);
  vtopen=0;
  trashtermsprite();
  closeced();
 }
}



void vtentertekmode(void)
{
 writetitle(VTERM,terminalname(transtoken("TK3")));
 refreshwindowtitle(whandle[VTERM]);
 vttekmode=1;
}


void vtquittekmode(void)
{
 writetitle(VTERM,terminalname(vtname));
 refreshwindowtitle(whandle[VTERM]);
 vttekmode=0;
}


